payolution.js ➔ payoneSwitchPayolution   B
last analyzed

Complexity

Conditions 7
Paths 11

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 59
rs 7.5346
c 1
b 0
f 0
cc 7
nc 11
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
function displayPayolutionOverlay(sCode)
2
{
3
    document.getElementById(sCode + '_overlay').style.display = "";
4
}
5
function removePayolutionOverlay(sCode)
6
{
7
    document.getElementById(sCode + '_overlay').style.display = "none";
8
}
9
10
function switchVisibility(aIds, blShow) 
11
{
12
    for(var i = 0; i < aIds.length; i++) {
13
        var oElement = $(aIds[i]);
14
        if(oElement) {
15
            if(blShow == true) {
16
                oElement.show();
17
            } else {
18
                oElement.hide();
19
            }
20
        }
21
    }
22
}
23
24
function payoneSwitchPayolution(oSelect, sCode) 
25
{
26
    if (oSelect == undefined) {
27
        return;
28
    }
29
30
    if(oSelect.value == 'PYV') {
31
        var aHide = [
32
            sCode + '_debit_wrap',
33
            sCode + '_debit_wrap2',
34
            sCode + '_installment_wrap1',
35
            sCode + '_installment_wrap2'
36
        ];
37
        var aShow = [
38
            sCode + '_b2b_wrap',
39
            sCode + '_birthday_wrap',
40
            sCode + '_acceptance_wrap'
41
        ];
42
        switchVisibility(aHide, false);
43
        switchVisibility(aShow, true);
44
        $(sCode + '_selected_installmentplan').value = '0';
45
    } else if(oSelect.value == 'PYD') {
46
        var aHide = [
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable aHide already seems to be declared on line 31. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
47
            sCode + '_installment_wrap1',
48
            sCode + '_installment_wrap2',
49
            sCode + '_debit_subwrap'
50
        ];
51
        var aShow = [
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable aShow already seems to be declared on line 37. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
52
            sCode + '_debit_wrap',
53
            sCode + '_debit_wrap2',
54
            sCode + '_b2b_wrap',
55
            sCode + '_birthday_wrap',
56
            sCode + '_acceptance_wrap'
57
        ];
58
        switchVisibility(aHide, false);
59
        switchVisibility(aShow, true);
60
        $(sCode + '_selected_installmentplan').value = '0';
61
    } else if(oSelect.value == 'PYS') {
62
        if(!$(sCode + '_installment_wrap2').visible()) {// reset installment init state
63
            var aHide = [
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable aHide already seems to be declared on line 31. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
64
                sCode + '_debit_wrap',
65
                sCode + '_debit_wrap2',
66
                sCode + '_debit_subwrap'
67
            ];
68
            var aShow = [
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable aShow already seems to be declared on line 37. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
69
                sCode + '_installment_wrap1'
70
            ];
71
            switchVisibility(aHide, false);
72
            switchVisibility(aShow, true);
73
            $(sCode + '_selected_installmentplan').value = '';
74
        }
75
    }
76
    
77
    if(oSelect.value == '') {
78
        $(sCode + '_main_block').hide();
79
    } else {
80
        $(sCode + '_main_block').show();
81
    }
82
}
83
84
function handleInstallmentAllowed(response) 
85
{
86
    $(response.code + '_installment_wrap2').update(response.update_section.html);
87
    
88
    var aHide = [
89
        response.code + '_b2b_wrap',
90
        response.code + '_birthday_wrap',
91
        response.code + '_acceptance_wrap',
92
        response.code + '_installment_wrap1'
93
    ];
94
    var aShow = [
95
        response.code + '_installment_wrap2'
96
    ];
97
    switchVisibility(aHide, false);
98
    switchVisibility(aShow, true);
99
}
100
101
function handleInstallment(sCode, sUrl) 
102
{
103
    if (checkout.loadWaiting!=false) return;
0 ignored issues
show
Bug introduced by
The variable checkout seems to be never declared. If this is a global, consider adding a /** global: checkout */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
104
    
105
    var validator = new Validation(payment.form);
0 ignored issues
show
Bug introduced by
The variable payment seems to be never declared. If this is a global, consider adding a /** global: payment */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
The variable Validation seems to be never declared. If this is a global, consider adding a /** global: Validation */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
106
    if (payment.validate() && validator.validate()) {
107
        checkout.setLoadWaiting('payment');
108
        
109
        var sDob = $(sCode + '_additional_fields_customer_dob_full').value;
110
        var sType = $(sCode + '_type_select').value;
111
        var sPaymentMethodId = $(sCode + '_payment_method_id').value;
112
113
        new Ajax.Request(
0 ignored issues
show
Unused Code Best Practice introduced by
The object created with new Ajax.Request(sUrl, {...alse,false,None,None)}) is not used but discarded. Consider invoking another function instead of a constructor if you are doing this purely for side effects.
Loading history...
Bug introduced by
The variable Ajax seems to be never declared. If this is a global, consider adding a /** global: Ajax */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
114
            sUrl, {
115
            method: 'Post',
116
            parameters: {
117
                payone_payolution_type : sType,
118
                payone_customer_dob : sDob,
119
                payone_config_payment_method_id : sPaymentMethodId,
120
                code : sCode
121
            },
122
            onComplete: function (transport) {
123
                checkout.setLoadWaiting(false);
0 ignored issues
show
Bug introduced by
The variable checkout seems to be never declared. If this is a global, consider adding a /** global: checkout */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
124
                if(transport.responseText) {
125
                    response = JSON.parse(transport.responseText);
0 ignored issues
show
Bug introduced by
The variable response seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.response.
Loading history...
126
                    if(response.success == true) {
127
                        handleInstallmentAllowed(response);
128
                        return;
129
                    }
130
                }
131
132
                alert(Translator.translate("The installment calculation failed. Please choose another payment type."));
0 ignored issues
show
Bug introduced by
The variable Translator seems to be never declared. If this is a global, consider adding a /** global: Translator */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
133
            }
134
            }
135
        );
136
    }
137
}
138
139
function switchInstallmentPlan(sKey, sCode, iInstallments) 
140
{
141
    $$('.payolution_installmentplans').each(
142
        function (e) {
143
          e.hide(); 
144
        } 
145
    );
146
    $$('.payolution_installment_overview').each(
147
        function (e) {
148
          e.hide(); 
149
        } 
150
    );
151
    
152
    var aShow = [
153
        'payolution_installmentplan_' + sKey,
154
        'payolution_installment_overview_' + sKey,
155
        sCode + '_debit_wrap',
156
        sCode + '_debit_subwrap'
157
    ];
158
    switchVisibility(aShow, true);
159
    $(sCode + '_selected_installmentplan').value = iInstallments;
160
}